home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / program / cgrphxdv.lha / CGraphX / Oberon / Examples / Window.mod
Text File  |  1995-12-01  |  9KB  |  309 lines

  1. (*------------------------------------------
  2.  
  3.   :Program.     Window
  4.   :Author.      Mario Kemper  [mk]
  5.   :Address.     Geiststrasse 53 , D-59555 Lippstadt
  6.   :EMail.       magick@bundy.lip.owl.de
  7.   :EMail.       magick@uni-paderborn.de
  8.   :Phone.       02941/5509
  9.   :Version.     V0.1
  10.   :Date.        30-Nov-1995
  11.   :Copyright.   Mario Kemper
  12.   :Language.    Oberon-2
  13.   :Translator.  Oberon 3.20 (03.08.94)
  14.   :Contents.    Demo for the Cybergraphics.library
  15.   :Remarks.     Original C-Source by Matthias Scheler (Tron)
  16.   :Remarks.     This is a rather clumsy straight port from the C-Example
  17.   :Usage.       Runs only from CLI (sorry, but i'm really lazy :-)
  18.   :History.     0.1    [mk] 30-Nov-1995 : Initial Release
  19.  
  20. --------------------------------------------*)
  21.  
  22.  
  23. MODULE Window;
  24.  
  25. IMPORT cgfx:=CyberGraphics,ol:=OberonLib,int:=Intuition,e:Exec,d:Dos,
  26.        s:=SYSTEM,u:=Utility,gfx:=Graphics,NoGuru;
  27.  
  28. (* SHORTSET, because SHORTINTS only go from -127 to 127 *)
  29. TYPE DataPtr = UNTRACED POINTER TO ARRAY (MAX(LONGINT) -1) OF CHAR;
  30.  
  31.  
  32. CONST screenWantedWidth  = 640;
  33.       screenWantedHeight = 480;
  34.       screenWantedDepth  =  24;
  35.       screenMinDepth     =  15;
  36.       imageWidth         = 256;
  37.       imageHeight        = 256;
  38.       imageBpp           =   3;
  39.  
  40.  
  41. PROCEDURE Min(a,b:INTEGER) : INTEGER;
  42. BEGIN
  43.   IF a<b THEN
  44.     RETURN(a);
  45.   ELSE
  46.     RETURN(b);
  47.   END;
  48. END Min;
  49.  
  50. PROCEDURE Next(x:CHAR):CHAR;
  51. (* return the next character or CHR(0), if ORD(x) > 255 *)
  52. (* this is needed, because a Shortint only ranges from -127 to 127*)
  53. BEGIN
  54.   IF ORD(x) < 255 THEN
  55.     x:=CHR(ORD(x)+1);
  56.   ELSE
  57.     x:=CHR(0);
  58.   END;
  59.   RETURN(x);
  60. END Next;
  61.  
  62. PROCEDURE CreateImageData(image : DataPtr;width,height:LONGINT);
  63.  
  64. VAR x,y,i:LONGINT;
  65.     r,g:CHAR;
  66.  
  67.  
  68. BEGIN
  69.   i:=0;r:=CHR(0);g:=CHR(0);
  70.   FOR y:=0 TO height-1 DO
  71.     FOR x:=0 TO width-1 DO
  72.       image[i] := r; (*red*)
  73.       INC(i);
  74.       image[i] := g; (*green*)
  75.       INC(i);
  76.       image[i] := CHR(0); (*blue*)
  77.       INC(i);
  78.       r:=Next(r);
  79.     END;
  80.     g:=Next(g);
  81.   END;
  82. END CreateImageData;
  83.  
  84.  
  85. (* window handling *)
  86.  
  87. PROCEDURE InnerWidth (window : int.WindowPtr):LONGINT;
  88. BEGIN
  89.   RETURN(window.width - window.borderLeft - window.borderRight);
  90. END InnerWidth;
  91.  
  92. PROCEDURE InnerHeight (window : int.WindowPtr):LONGINT;
  93. BEGIN
  94.   RETURN(window.height - window.borderTop - window.borderBottom);
  95. END InnerHeight;
  96.  
  97.  
  98. PROCEDURE RedrawScaleWindow (scaleWindow : int.WindowPtr; imageData : DataPtr);
  99. VAR long:LONGINT;
  100.  
  101. BEGIN
  102.   long:=cgfx.ScalePixelArray(imageData,imageWidth,imageHeight,imageWidth*imageBpp,
  103.                        scaleWindow.rPort,scaleWindow.borderLeft,scaleWindow.borderTop,
  104.                        SHORT(InnerWidth(scaleWindow)), SHORT(InnerHeight(scaleWindow)),
  105.                        cgfx.rectFmtRGB);
  106. END RedrawScaleWindow;
  107.  
  108.  
  109. PROCEDURE RedrawWriteWindow(writeWindow : int.WindowPtr;imageData : DataPtr);
  110. VAR long:LONGINT;
  111.  
  112. BEGIN
  113.   long:=cgfx.WritePixelArray(imageData,0,0,imageWidth*imageBpp,
  114.                              writeWindow.rPort,writeWindow.borderLeft,writeWindow.borderTop,
  115.                              SHORT(InnerWidth(writeWindow)),
  116.                              SHORT(InnerHeight(writeWindow)),
  117.                              cgfx.rectFmtRGB);
  118. END RedrawWriteWindow;
  119.  
  120. (* screen depth fallback *)
  121.  
  122. PROCEDURE NextDepth (depth : LONGINT):LONGINT;
  123. VAR
  124. result:LONGINT;
  125.  
  126. BEGIN
  127.   CASE depth OF
  128.   |24 : result:=16;
  129.   |16 : result:=15;
  130.   ELSE
  131.    result:=0;
  132.   END;
  133.   RETURN(result);
  134. END NextDepth;
  135.  
  136. (* main program *)
  137.  
  138. VAR
  139.   displayID,depth : LONGINT;
  140.   cyberScreen     : int.ScreenPtr;
  141.   scaleWindow,
  142.   writeWindow     : int.WindowPtr;
  143.   imageData       : DataPtr;
  144.   done            : BOOLEAN;
  145.   signal          : LONGSET;
  146.   bool            : BOOLEAN;
  147.   intMsg          : int.IntuiMessagePtr;
  148. BEGIN
  149.   done := FALSE;
  150.   depth := screenWantedDepth;
  151.   WHILE ((depth # 0) AND (NOT done)) DO
  152.     displayID:=cgfx.BestCModeIDTags(cgfx.bIDTGNominalWidth,screenWantedWidth,
  153.                                     cgfx.bIDTGNominalHeight,screenWantedHeight,
  154.                                     cgfx.bIDTGDepth,depth,
  155.                                     u.done);
  156.     IF displayID # gfx.invalidID THEN
  157.       depth:=cgfx.GetCyberIDAttr(cgfx.idAttrDepth,displayID);
  158.       done:=TRUE;
  159.     ELSE
  160.       depth:=NextDepth(depth);
  161.     END;
  162.  
  163.   END (*WHILE*);
  164.  
  165.   IF depth < screenMinDepth THEN
  166.     d.PrintF("Can't find suitable display mode for %ldx%ldx%ld.",
  167.              screenWantedWidth,screenWantedHeight,screenWantedDepth);
  168.     HALT(5);
  169.  
  170.   END;
  171.  
  172. (* open screen, but let intuition choose the actual dimensions *)
  173.  
  174.   cyberScreen:=int.OpenScreenTagsA(NIL,int.saTitle,s.ADR("CyberGraphX Demo"),
  175.                                   int.saDisplayID,displayID,
  176.                                   int.saDepth,depth,u.done);
  177.   IF cyberScreen = NIL THEN
  178.     d.PrintF("Can't open screen");
  179.     HALT(5);
  180.   END;
  181.  
  182.   (* create scale Window *)
  183.  
  184.   scaleWindow:=int.OpenWindowTagsA(NIL,
  185.                                   int.waTitle,s.ADR("Scale"),
  186.                                   int.waFlags,LONGSET{int.activate,int.simpleRefresh,
  187.                                     int.windowSizing,int.rmbTrap,int.windowDrag,
  188.                                     int.windowDepth,int.windowClose},
  189.                                   int.waIDCMP,LONGSET{
  190.                                    int.closeWindow,int.refreshWindow,
  191.                                    int.sizeVerify,int.newSize},
  192.                                   int.waLeft,16,
  193.                                   int.waTop,cyberScreen.barHeight+16,
  194.                                   int.waWidth,imageWidth,
  195.                                   int.waHeight,imageHeight,
  196.                                   int.waCustomScreen,cyberScreen,
  197.                                   u.done);
  198.  
  199.   IF scaleWindow = NIL THEN
  200.     d.PrintF("Can't open scale window.");
  201.     HALT(5);
  202.   END;
  203.  
  204.   IF int.WindowLimits(scaleWindow,
  205.                       scaleWindow.borderLeft+scaleWindow.borderRight+1,
  206.                       scaleWindow.borderTop+scaleWindow.borderBottom+1,
  207.                       cyberScreen.width,cyberScreen.height) THEN
  208.   END;
  209.  
  210.   (* create Write window *)
  211.  
  212.   writeWindow:=int.OpenWindowTagsA(NIL,
  213.                                   int.waTitle,s.ADR("Write"),
  214.                                   int.waFlags,LONGSET{int.activate,int.simpleRefresh,
  215.                                     int.windowSizing,int.rmbTrap,int.windowDrag,
  216.                                     int.windowDepth,int.windowClose},
  217.                                   int.waIDCMP,LONGSET{
  218.                                    int.closeWindow,int.refreshWindow,
  219.                                    int.sizeVerify,int.newSize},
  220.                                   int.waLeft,cyberScreen.width-16-imageWidth,
  221.                                   int.waTop,cyberScreen.barHeight+16,
  222.                                   int.waWidth,imageWidth,
  223.                                   int.waHeight,imageHeight,
  224.                                   int.waCustomScreen,cyberScreen,
  225.                                   u.done);
  226.  
  227.   IF writeWindow = NIL THEN
  228.     d.PrintF("Can't open write window.");
  229.     HALT(5);
  230.   END;
  231.  
  232.   IF int.WindowLimits(writeWindow,
  233.                       writeWindow.borderLeft+writeWindow.borderRight+1,
  234.                       writeWindow.borderTop+writeWindow.borderBottom+1,
  235.                       Min(cyberScreen.width,writeWindow.borderLeft+
  236.                           writeWindow.borderRight+imageWidth),
  237.                       Min(cyberScreen.height,writeWindow.borderTop+
  238.                           writeWindow.borderBottom+imageHeight)) THEN
  239.   END;
  240.  
  241.   (* allocate and create image data *)
  242.  
  243.   imageData:=e.AllocVec(imageWidth*imageHeight*imageBpp, LONGSET{e.public});
  244.   IF imageData = NIL THEN
  245.     d.PrintF("out of memory.");
  246.     HALT(5);
  247.   END;
  248.  
  249.   CreateImageData (imageData,imageWidth,imageHeight);
  250.  
  251. (* event loop *)
  252.  
  253.   RedrawScaleWindow(scaleWindow,imageData);
  254.   RedrawWriteWindow(writeWindow,imageData);
  255.  
  256.   done:=FALSE;
  257.  
  258.   WHILE(NOT done) DO
  259.     signal:=e.Wait(LONGSET{scaleWindow.userPort.sigBit,writeWindow.userPort.sigBit});
  260.     intMsg:=e.GetMsg(scaleWindow.userPort);
  261.     WHILE (intMsg#NIL) DO
  262.       IF int.refreshWindow IN intMsg.class THEN
  263.          int.BeginRefresh(scaleWindow);
  264.          RedrawScaleWindow(scaleWindow,imageData);
  265.          int.EndRefresh(scaleWindow,int.LTRUE);
  266.       END;
  267.       IF int.newSize IN intMsg.class THEN
  268.          RedrawScaleWindow(scaleWindow,imageData);
  269.       END;
  270.       IF int.closeWindow IN intMsg.class THEN
  271.          done:=TRUE;
  272.       END;
  273.  
  274.       e.ReplyMsg(intMsg);
  275.       intMsg:=e.GetMsg(scaleWindow.userPort);
  276.     END (*WHILE*);
  277.  
  278.     intMsg:=e.GetMsg(writeWindow.userPort);
  279.  
  280.     WHILE (intMsg#NIL) DO
  281.  
  282.       IF int.refreshWindow IN intMsg.class THEN
  283.          int.BeginRefresh(writeWindow);
  284.          RedrawWriteWindow(writeWindow,imageData);
  285.          int.EndRefresh(writeWindow,int.LTRUE);
  286.       END;
  287.       IF int.newSize IN intMsg.class THEN
  288.          RedrawWriteWindow(writeWindow,imageData);
  289.       END;
  290.  
  291.       IF int.closeWindow IN intMsg.class THEN
  292.          done:=TRUE;
  293.       END;
  294.       e.ReplyMsg(intMsg);
  295.       intMsg:=e.GetMsg(writeWindow.userPort);
  296.     END (*WHILE *);
  297.  
  298.   END (*WHILE*);
  299.  
  300.   (* CleanUP *)
  301. CLOSE
  302.  
  303.   IF imageData   # NIL THEN e.FreeVec (imageData) END;
  304.   IF writeWindow # NIL THEN int.CloseWindow(writeWindow) END;
  305.   IF scaleWindow # NIL THEN int.CloseWindow (scaleWindow) END;
  306.   IF cyberScreen # NIL THEN bool:=int.CloseScreen (cyberScreen) END;
  307.  
  308. END Window.
  309.